import requests
import sqlite3


def get_en():
    db = requests.get('https://db.ygoprodeck.com/api/v7/cardinfo.php')
    data_lst = db.json()['data']
    con = sqlite3.connect('name_id.db')
    cursor = con.cursor()
    cursor.execute('CREATE TABLE IF NOT EXISTS data (name TEXT PRIMARY KEY, id INTEGER NOT NULL);')

    if len(data_lst) > 10000:
        cursor.execute('DELETE FROM data;')
        i = 0
        for data in data_lst:
            sql = 'INSERT INTO data VALUES ("{}", {});'.format(data['name'].replace('"', '""'), data['id'])
            cursor.execute(sql)
            i += 1
            print(data)
            print('\r更新进度：[{}/{}]'.format(i, len(data_lst)), end='')

    con.commit()
    cursor.close()
    con.close()
